package save

import (
	"bufio"
	"image"
	"image/png"
	"os"
)

func SaveInPng(address string, img image.Image) error {
	if address == "" {
		address = "./unknow.png"
	}
	file, err := os.OpenFile(address, os.O_CREATE|os.O_WRONLY, 0777)
	if err != nil {
		return err
	}
	defer file.Close()
	b := bufio.NewWriter(file)
	err = png.Encode(b, img)
	if err != nil {
		return err
	}
	err = b.Flush()
	if err != nil {
		return err
	}
	return nil
}
